package org.example.wine.boot.controller;

import jakarta.validation.constraints.Pattern;
import org.example.wine.boot.pojo.Result;
import org.example.wine.boot.pojo.User;
import org.example.wine.boot.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/user")
@Validated
public class UserController {
    private static final String REGEX_TIP_USERNAME = "用户名长度必须在5到16位之间";
    private static final String REGEX_TIP_PASSWORD = "密码长度必须在5到16位之间";

    @Autowired
    private UserService userService;


    @GetMapping("/register")
    public Result register(@Pattern(regexp = "^\\w{5,16}$", message = REGEX_TIP_USERNAME) String username, @Pattern(regexp = "^\\w{5,16}$", message = REGEX_TIP_PASSWORD) String password) {
        return userService.register(username, password);
    }

    @GetMapping("/findByUsername")
    public User findByUsername(String username) {
        return userService.findByUsername(username);
    }

    @GetMapping("/login")
    public Result login(@Pattern(regexp = "^\\w{5,16}$", message = REGEX_TIP_USERNAME) String username, @Pattern(regexp = "^\\w{5,16}$", message = REGEX_TIP_PASSWORD) String password) {
        return userService.login(username, password);
    }


}
